package bll

import (
	"context"

	"github.com/LyricTian/gin-admin/v7/internal/app/schema"
)

func (a *Like) SetLike(ctx context.Context, item schema.Like) (like int) {
	data, err := a.VideoModel.Get(ctx, item.VideoId)
	if err != nil {
		return 0
	}
	if data == nil {
		return 0
	}

	if a.LikeModel.IsLike(ctx, item.VideoId, item.ContentType) {
		like, _ = a.LikeModel.GetLikeNum(ctx, item.VideoId, item.ContentType)
		return
	}
	// create like
	a.LikeModel.Create(ctx, item)
	t := 0
	if item.Type == 1 {
		if data.ContentType == 1 {
			t = 3
		}
		if data.ContentType == 2 {
			t = 4
		}
	}
	if item.Type == 2 {
		if data.ContentType == 1 {
			t = 5
		}
		if data.ContentType == 2 {
			t = 6
		}
	}

	// add notify
	a.GroupNotificationServer.Create(ctx, schema.GroupNotification{
		GroupId:       item.GroupId,
		ContentId:     item.VideoId,
		NotifyImage:   "",
		NotifyContent: "语音点赞",
		Type:          t,
		Creator:       item.Creator,
	})

	like, _ = a.LikeModel.GetLikeNum(ctx, item.VideoId, item.ContentType)
	return
}
